package com.rms.hkpc.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rms.hkpc.R;
import com.rms.hkpc.adapter.DialogAdapter;
import com.rms.hkpc.adapter.base.BaseAdapter;
import com.rms.hkpc.db.dto.ShortDto;

import java.util.List;

public class ShortDialog extends BaseDialog {

    private Context context;
    private List<ShortDto> listShort;
    private String selectId;
    private int title;

    public ShortDialog(@NonNull Context context, List<ShortDto> listShort, int title, String selectId, IChoiceListener iChoiceListener) {
        super(context, R.style.toast_dialog);
        this.context = context;
        this.listShort = listShort;
        this.iChoiceListener = iChoiceListener;
        this.title = title;
        this.selectId = selectId;
    }

    @Override
    public void refreshView() {

    }

    @Override
    public void initView() {
        TextView tv = findViewById(R.id.tv_title);
        tv.setText(title);
        RecyclerView rv = findViewById(R.id.rv_project);
        for (int i = 0; i < listShort.size(); i++) {
            if (listShort.get(i).getId().equals(selectId)) {
                listShort.get(i).setSelect(true);
                break;
            }
        }
        final DialogAdapter adapter = new DialogAdapter(context, listShort, R.layout.rvitem_dialog);
        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View v, int position) {
                adapter.changeSelect(position);
                iChoiceListener.getChoiceFromDialog(listShort.get(position));
                dismiss();
            }
        });
        rv.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        rv.setAdapter(adapter);
        rv.setItemAnimator(null);
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_rv;
    }

    private IChoiceListener iChoiceListener;

    public interface IChoiceListener {
        void getChoiceFromDialog(ShortDto bean);
    }
}
